home *** CD-ROM | disk | FTP | other *** search
- /* MacTCP finger client */
- /* written by Steven Falkenburg */
- /* */
- /* 10/5/90 original coding */
-
- #include "compat.h"
-
- #ifdef PROTOS
- #include <Types.h>
- #include <Memory.h>
- #include <Packages.h>
- #include <CursorCtl.h>
- #endif
-
- #include <String.h>
-
- #include "CvtAddr.h"
- #include "MacTCPCommonTypes.h"
- #include "TCPPB.h"
- #include "TCPHi.h"
-
-
- /* constants */
-
- #define kFingerPort 79 /* TCP port assigned for finger protocol */
- #define kBufSize 16384 /* Size for TCP stream buffer and receive buffer */
- #define kTimeOut 20 /* Timeout for TCP commands */
-
-
- /* fuction prototypes */
-
- void main(int argc,char *argv[]);
- OSErr Finger(char *userid,char *hostName,Handle *fingerData);
- OSErr GetFingerData(unsigned long stream,Handle *fingerData);
- void FixCRLF(char *data);
- Boolean GiveTime(short sleepTime);
-
- /* globals */
-
- Boolean gCancel = false; /* this is set to true if the user cancels an operation */
-
- /* main entry point for finger */
- /* */
- /* usage: finger <user>@<host> */
- /* */
- /* this function parses the args from the command line, */
- /* calls Finger() to get info and prints the returned info. */
-
- void main(int argc,char *argv[])
- {
- OSErr err;
- Handle theFinger;
- char userid[256],host[256];
-
- if (argc != 2) {
- printf("Wrong number of parameters to finger call\n");
- return;
- }
-
- sscanf(argv[1],"%[^@]@%s",userid,host);
-
- strcat(userid,"\n\r");
-
- err = Finger(userid,host,&theFinger);
-
- if (err == noErr) {
- HLock(theFinger);
- FixCRLF(*theFinger);
- printf("\n%s\n",*theFinger);
- DisposHandle(theFinger);
- }
- else
- printf("An error has occurred: %hd\n",err);
- }
-
-
- /* Finger() */
- /* */
- /* This function converts the host string to an IP number, opens a */
- /* connection to the remote host on TCP port 79, sends the id to */
- /* the remote host and waits for the information on the receiving */
- /* stream. After this information is sent, the connection is */
- /* is closed down. */
-
- OSErr Finger(char *userid,char *hostName,Handle *fingerData)
- {
- OSErr err;
- unsigned long ipAddress;
- unsigned long stream;
-
- /* open the network driver */
-
- err = InitNetwork();
- if (err!=noErr)
- return err;
-
- /* get remote machine's network number */
-
- err = ConvertStringToAddr(hostName,&ipAddress);
- if (err!=noErr)
- return err;
-
- /* open a TCP stream */
-
- err = CreateStream(&stream,kBufSize);
- if (err!=noErr)
- return err;
-
- err = OpenConnection(stream,ipAddress,kFingerPort,kTimeOut);
- if (err==noErr) {
- err = SendData(stream,userid,(unsigned short)strlen(userid),false);
- if (err==noErr)
- err = GetFingerData(stream,fingerData);
- CloseConnection(stream);
- }
-
- ReleaseStream(stream);
- return err;
- }
-
-
- OSErr GetFingerData(unsigned long stream,Handle *fingerData)
- {
- OSErr err;
- long bufOffset = 0;
- unsigned short dataLength;
- Ptr data;
-
- *fingerData = NewHandle(kBufSize);
- err = MemError();
- if (err!=noErr)
- return err;
-
- HLock(*fingerData);
- data = **fingerData;
- dataLength = kBufSize;
-
- do {
- err = RecvData(stream,data,&dataLength,false);
- if (err==noErr) {
- bufOffset += dataLength;
- dataLength = kBufSize;
- HUnlock(*fingerData);
- SetHandleSize(*fingerData,bufOffset+kBufSize);
- err = MemError();
- HLock(*fingerData);
- data = **fingerData + bufOffset;
- }
- } while (err==noErr);
-
- data[0] = '\0';
-
- HUnlock(*fingerData);
- if (err == connectionClosing)
- err = noErr;
- }
-
-
- /* FixCRLF() removes the linefeeds from a */
- /* text buffer. This is necessary, since */
- /* all text on the network is embedded with */
- /* carraige return linefeed pairs. */
-
- void FixCRLF(char *data)
- {
- register char *source,*dest;
- long length;
-
- length = strlen(data);
-
- if (*data) {
- source = dest = data;
- while ((source - data) < (length-1)) {
- if (*source == '\r')
- source++;
- *dest++ = *source++;
- }
- if (*source != '\r' && (source - data) < length)
- *dest++ = *source++;
- length = dest - data;
- }
-
- *dest = '\0';
- }
-
-
- /* this routine would normally be a callback for giving time to background
- apps */
-
- Boolean GiveTime(short sleepTime)
- {
- SpinCursor(1);
- return true;
- }